package com.epoint.action.activityinfo;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.epoint.bizlogic.activityinfo.ActivityInfoService;
import com.epoint.bizlogic.activityinfo.domain.ActivityInfo;
import com.epoint.bizlogic.activityinfo.domain.ActivityInfoVo;
import com.epoint.utils.RequestUtils;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;

/**
 * Servlet implementation class ActivityListAction
 */
@WebServlet("/activitylistaction")
public class ActivityListAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	ActivityInfoService activityInfoService = new ActivityInfoService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ActivityListAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String requestMethod = request.getParameter("method");
		String json = RequestUtils.reflectInvokMethod(requestMethod, request, ActivityListAction.class);
		response.getWriter().write(json);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	
	/**
     * 查询活动信息 
     * @param pageIndex 页号
     * @param pageSize 页面大小
     * @return
     */
    public String queryActivityInfo(HttpServletRequest request) {
        String pageIndex = request.getParameter("pageIndex");
        String pageSize = request.getParameter("pageSize");
        List<ActivityInfoVo> lsit = activityInfoService.queryActivityInfo(Integer.valueOf(pageIndex), Integer.valueOf(pageSize));
        int total = activityInfoService.queryActivityInfo(null, null).size();
        Map<String, Object> reslut = new HashMap<>(2);
        reslut.put("total", total);
        reslut.put("data", lsit);
        return JSONObject.toJSONStringWithDateFormat(reslut, "yyyy-MM-dd HH:mm");
    }
    
    /**
     * 获取活动id
     * @param request
     * @return
     */
    public String getActivityId(HttpServletRequest request) {
        String clubId = request.getParameter("clubId");
        String clubType = request.getParameter("clubType");
        String activityId = activityInfoService.getActivityId(clubId);
        if (StrUtil.isBlankIfStr(activityId)) {
            activityId = clubType + DateUtil.year(new Date()) + "001";
        } else {
            activityId = clubType + activityId;
        }
        
        return JSONObject.toJSONString(activityId);
    }

}
